package com.wedu.modules.dss;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wedu.modules.dss.dao.DssDeviceInformationDao;
import com.wedu.modules.dss.dao.DssOperationStatusDao;
import com.wedu.modules.dss.entity.DssDeviceInformationEntity;
import com.wedu.modules.dss.entity.DssDeviceProductionEntity;
import com.wedu.modules.dss.entity.OperationStatusEntity;
import com.wedu.modules.dss.entity.dto.DssDeviceInformationDTO;
import com.wedu.modules.dss.service.DssDeviceInformationService;
import com.wedu.modules.dss.service.DssOperationStatusService;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 *  数据监听器
 */
public class ExcelImportListener extends AnalysisEventListener<DssDeviceInformationDTO> {
private  final DssOperationStatusService dssOperationStatusService;
    private final DssDeviceInformationService service;
    private  List<String> deviceIdList;
    private DssDeviceInformationDao dssDeviceInformationDao;
    private int successCount = 0;
    private int errorCount = 0;
    private String errorNull="上传的表格中有未填写的内容";

    public ExcelImportListener(DssOperationStatusService dssOperationStatusService, DssDeviceInformationService service, List<String> deviceIdList,DssDeviceInformationDao dssDeviceInformationDao) {
        this.dssDeviceInformationDao =dssDeviceInformationDao;
        this.dssOperationStatusService=dssOperationStatusService;
        this.service = service;
        this.deviceIdList=deviceIdList;
    }

//
//    private ArrayList<String>  successDeviceIdList = new ArrayList<>();
//    private ArrayList<String>  errorDeviceIdList = new ArrayList<>();

    //批量读取 Excel 时逐行解析的回调方法
    @Override
    public void invoke(DssDeviceInformationDTO data, AnalysisContext context) {
        try {


if (!StringUtils.isBlank(data.getDeviceId())) {

    if (!deviceIdList.contains(data.getDeviceId())) {

        DssDeviceInformationEntity dssDeviceInformationEntity = new DssDeviceInformationEntity();
        dssDeviceInformationEntity.setDeviceId(data.getDeviceId());
        dssDeviceInformationEntity.setDeviceName(data.getDeviceName());
        dssDeviceInformationEntity.setDeviceModel(data.getDeviceModel());
        dssDeviceInformationEntity.setFactory(data.getFactory());
        dssDeviceInformationEntity.setProduceTime(data.getProduceTime());
        HashMap<String, Object> map = service.dictAndUserQuery(data);
        dssDeviceInformationEntity.setDeviceType((String) map.get("type"));
        dssDeviceInformationEntity.setCreateBy((Long) map.get("userId"));
        dssDeviceInformationEntity.setNotes(data.getNotes());
        dssDeviceInformationEntity.setStartUseTime(data.getStartUseTime());
        dssDeviceInformationEntity.setMaintenanceTime(data.getMaintenanceTime());
        dssDeviceInformationEntity.setSensorId(data.getSensorId());
        dssDeviceInformationEntity.setWorkshopName(data.getWorkshopName());
        dssDeviceInformationEntity.setCreateTime(data.getCreateTime());
       dssDeviceInformationEntity.setMaintenanceCycle(480);
        dssDeviceInformationEntity.setMaintenancePlan("2");
        dssDeviceInformationEntity.setNoticeTime(new Date());
        dssDeviceInformationEntity.setMaintenanceDueTime(new Date());
        //    list.add(dssDeviceInformationEntity);


        // 存数据库
        service.save(dssDeviceInformationEntity);
        QueryWrapper<DssDeviceInformationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id", data.getDeviceId());
        DssDeviceInformationEntity dssDeviceInformation = dssDeviceInformationDao.selectOne(queryWrapper);
        OperationStatusEntity dssOperationStatus = new OperationStatusEntity();
        dssOperationStatus.setLightStatus(1);
        dssOperationStatus.setLightStartTime(data.getStartUseTime());
        dssOperationStatus.setDeviceId(dssDeviceInformation.getId());
        dssOperationStatusService.save(dssOperationStatus);
        //成功的条数
        successCount++;

        // 存储完成清理 list
        //   list.clear();
    }
}           //失败的条数
            errorCount++;
        } catch (Exception e) {
            //表示有内容为空
            errorNull=errorNull+data.getDeviceId()+" ";
        }
    }
    public Integer getSuccessCount(){
        return successCount;
    }
    public Integer getErrorCount(){
        return errorCount-1;
    }
    public String getErrorNull(){
        return errorNull;
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {}
//
//    // 向数据库插入数据
//    private void saveData() {
//        System.out.println("开始存储数据库！");
//
//
//        System.out.println("存储数据库成功！");
//    }
}